﻿Public Class HistoryDataClass

#Region "DealData"

    Public Shared Function DealData(ByVal dt As DateTime, ByVal buf() As Byte, ByVal ProjectID As String, ByVal netID As Byte) As Boolean

        If buf Is Nothing Then
            Exit Function
        End If

        If Not UtilityClass.BufferIsValidate(buf) Then
            Exit Function
        End If

        Dim tic As New TranstrationItem

        Dim strSql As String = String.Format("Insert Into tbccHDData_{0}_{1}(hDate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12," _
            & "Ref1_RefAlarmState,Ref1_LostPowerState,Ref1_SoundOutputState,Ref1_DoorState,Ref2_RefAlarmState,Ref2_LostPowerState,Ref2_SoundOutputState,Ref2_DoorState," _
            & "Ref3_RefAlarmState,Ref3_LostPowerState,Ref3_SoundOutputState,Ref3_DoorState,Ref4_RefAlarmState,Ref4_LostPowerState,Ref4_SoundOutputState,Ref4_DoorState)" _
                & " Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ; --", ProjectID.Replace("-", "_"), netID.ToString)

        For i As Integer = 0 To HIST_COUNT_EACH_PAGE - 1

            If UtilityClass.BufferIsValidate(buf, i * HIST_COUNT_EACH_TIME, HIST_COUNT_EACH_TIME) = False Then
                '此条记录的数据为全0或者全1 （255），视为无效数据
                Continue For
            End If

            Dim p(28) As SQLiteParameter
            For pi As Integer = 0 To p.Length - 1
                p(pi) = New SQLiteParameter()
            Next

            p(0).Value = UtilityClass.GetDateString(dt.AddSeconds(i * HIST_SAVE_INTERVAL_TIME))


            '报警状态
            For j As Integer = 0 To 3  '各个冷库报警状态
                For k As Integer = 0 To 3  '当前冷库各个报警
                    p(13 + j * 4 + k).Value = GetBit(buf(i * HIST_COUNT_EACH_TIME + j), k).ToString
                Next
            Next

            '温度/湿度值
            For j As Integer = 0 To 11
                p(j + 1).Value = Int2Single(buf(i * HIST_COUNT_EACH_TIME + 2 * j + 4), buf(i * HIST_COUNT_EACH_TIME + 2 * j + 5)).ToString()
            Next

            tic.Add(strSql & i, p)
        Next

        '增加已上传报警库的标识信息

        If tic.Count > 0 Then
            strSql = String.Format("Insert Into tbccHistoryData_{0}(netID,iYear,iMonth,iDay,iHour,iMinute,iSecond,uploadTime) Values(?,?,?,?,?,?,?,?)", Trim(ProjectID).Replace("-", "_"))

            Dim p1(7) As SQLite.SQLiteParameter
            For i As Integer = 0 To p1.Length - 1
                p1(i) = New SQLiteParameter()
            Next

            p1(0).Value = netID
            p1(1).Value = dt.Year
            p1(2).Value = dt.Month
            p1(3).Value = dt.Day
            p1(4).Value = dt.Hour
            p1(5).Value = dt.Minute
            p1(6).Value = dt.Second
            p1(7).Value = UtilityClass.GetDateString(Now)
            tic.Add(strSql, p1)

            SqlHelper.ExecuteTranstration(tic)

        End If

    End Function

#End Region


    '#Region "DealFinish"

    'Public Shared Function DealFinish(ByVal pid As String, ByVal dt As DateTime, ByVal netID As Byte) As Boolean
    '    Dim iYear As Integer
    '    Dim iMonth As Integer
    '    Dim iDay As Integer
    '    Dim iHour As Integer
    '    Dim iMinute As Integer
    '    Dim iSecond As Integer

    '    iYear = dt.Year
    '    iMonth = dt.Month
    '    iDay = dt.Day
    '    iHour = dt.Hour
    '    iMinute = dt.Minute
    '    iSecond = dt.Second

    '    Dim strSql As String
    '    strSql = String.Format("Insert Into tbccHistoryData_{0}(netID,iYear,iMonth,iDay,iHour,iMinute,iSecond,uploadTime) Values(?,?,?,?,?,?,?,?)", Trim(pid).Replace("-", "_"))

    '    Dim p(7) As SQLite.SQLiteParameter
    '    For i As Integer = 0 To p.Length - 1
    '        p(i) = New SQLiteParameter()
    '    Next

    '    p(0).Value = netID
    '    p(1).Value = dt.Year
    '    p(2).Value = dt.Month
    '    p(3).Value = dt.Day
    '    p(4).Value = iHour
    '    p(5).Value = iMinute
    '    p(6).Value = iSecond
    '    p(7).Value = UtilityClass.GetDateString(Now)

    '    Dim icount As Integer
    '    icount = SqlHelper.ExecuteNonQuery(strSql, p)

    '    Return icount = 1
    'End Function

    '#End Region


    ''' <summary>
    ''' 温度的Int到Float的转换
    ''' </summary>
    ''' <param name="iValue"></param>
    ''' <returns></returns>
    ''' <remarks>将读到的Int值(2字节int)除以100得到对应的浮点值</remarks>
    Private Shared Function Int2Single(ByVal iValue As Short) As Single
        Return iValue * 0.01
    End Function

    Private Shared Function Int2Single(ByVal b1 As Byte, ByVal b2 As Byte) As Single
        Dim s As Int16
        Dim b(1) As Byte
        b(0) = b1
        b(1) = b2
        s = BitConverter.ToInt16(b, 0)
        Return s * 0.01
    End Function


    ''' <summary>
    ''' 判断指定位是否为1
    ''' </summary>
    ''' <param name="b"></param>
    ''' <param name="i"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function GetBit(ByVal b As Byte, ByVal i As Integer) As Byte
        Dim iTest As Byte
        iTest = 2 ^ i

        Dim result As Byte
        result = b And iTest

        If result > 0 Then
            result = 1
        End If

        Return result
    End Function


    ''' <summary>
    ''' 温度的Int到Float的转换
    ''' </summary>
    ''' <param name="iValue"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function Temperature_Int2Single(ByVal iValue As UShort) As Single
        Return iValue
    End Function

End Class
